package com.zekdot.userclient.service.impl;

import com.zekdot.userclient.dao.UserDao;
import com.zekdot.userclient.dto.Envelope;
import com.zekdot.userclient.entity.UserEntity;
import com.zekdot.userclient.service.UserService;
import com.zekdot.userclient.util.ConfigInfo;
import com.zekdot.userclient.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * Create by zekdot on 20-3-14.
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;
    @Autowired
    private ConfigInfo configInfo;
    @Override
    public Envelope addUser(UserEntity userEntity, int joinMet){
        userEntity.setPassword(PasswordUtil.getMd5(userEntity.getPassword()));
        userEntity.setJoinTime(new Timestamp(new Date().getTime()));
        userEntity.setJoinMet(joinMet);
        userEntity.setIdentify(0);
        userDao.save(userEntity);
        return Envelope.success;
    }

    @Override
    public Envelope getUser(String username) {
        UserEntity userEntity = null;
        userEntity = userDao.get(username);
        if(userEntity == null)
            return new Envelope(1,"用户不存在",null);
        userEntity.setPassword(null);
        return new Envelope(userEntity);
    }

    @Override
    public Envelope addUsers(List<UserEntity> userEntityList) {

        for(UserEntity userEntity:userEntityList){
            Envelope envelope  = addUser(userEntity,1);
            if(envelope.getCode() != 0)
                return envelope;
        }
        return Envelope.success;
    }

    @Override
    public Envelope register(UserEntity userEntity) throws UnsupportedEncodingException {
        Envelope envelope = null;
        try{
            envelope = addUser(userEntity,0);
        }catch(Exception e){
//            e.printStackTrace();

            if(e instanceof DuplicateKeyException){
                return new Envelope(1,"该用户名已被注册",null);
            }else{
                throw e;
            }
        }

        if(envelope.getCode() != 0)
            return envelope;
        envelope.setToken(PasswordUtil.getToken(userEntity,configInfo.ttlMills));
        userEntity.setPassword(null);
        return envelope;
    }

    @Override
    public Envelope login(String username, String password) throws UnsupportedEncodingException {
        UserEntity userEntity = null;
        try{
            userEntity = userDao.get(username);
        }catch (Exception e){
            e.printStackTrace();
            return Envelope.dbError;
        }
        if(userEntity == null){
            return new Envelope(1,"用户不存在",null);
        }
        if(!userEntity.getPassword().equals(PasswordUtil.getMd5(password))){
            return new Envelope(2,"用户名或密码错误",null);
        }
        if(userEntity.getIdentify() == 2){
            return new Envelope(3,"用户被限制登录",null);
        }
        String token;
        token = PasswordUtil.getToken(userEntity,configInfo.ttlMills);
        userEntity.setPassword(null);
        return new Envelope(0,"success",userEntity,token);
    }

    @Override
    public Envelope updateUserState(String username, int status) {
        if(status == 2){
            userDao.stopUser(username);
        }else if(status == 0){
            userDao.restartUser(username);
        }else if(status == 1){
            userDao.delete(username);
        }
        return Envelope.success;
    }

    @Override
    public Envelope getMoreUsers(String lastUsername, int length) {
        List<UserEntity> list = userDao.getMoreUsers(lastUsername,length);
        return new Envelope(list);
    }

    @Override
    public Envelope changePassword(String username, String oriPassword, String newPassword) {
        UserEntity userEntity = userDao.get(username);
        if(userEntity == null){
            return new Envelope(1,"用户不存在",null);
        }
        if(!PasswordUtil.getMd5(oriPassword).equals(userEntity.getPassword())){
            return new Envelope(2,"原密码输入错误",null);
        }
        newPassword = PasswordUtil.getMd5(newPassword);
        userDao.updatePassword(username,newPassword);
        return Envelope.success;
    }
}
